package com.example.siyun.controller.coursecontrooller;

import com.example.siyun.pojo.CourseOperateLog;
import com.example.siyun.pojo.EnumPojo;
import com.example.siyun.service.courseoperatelogservice.CourseOperateLogService;
import com.example.siyun.util.PageResult;
import com.example.siyun.util.WebUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Auther: 全
 * @Date: 2022/9/26 09:33
 * @Description: 课程操作的控制成
 */
@Slf4j
@RestController
public class CourseOperateController {
    @Autowired
    private CourseOperateLogService courseOperateLogService;
    /**
     * 获取课程操作日志
     */
    @RequestMapping("/advanced/gettablelog")
    public PageResult getAll(int page, int limit, CourseOperateLog courseoperatelog) {
        log.info("获取课程操作日志");
        PageHelper.startPage(page, limit);
        List<CourseOperateLog> list = courseOperateLogService.findCourseOperate(courseoperatelog, EnumPojo.VALUE_DEL_0);

        PageInfo<CourseOperateLog> pageInfo = new PageInfo<>(list);
        PageResult pageResult=new PageResult();
        pageResult.setState(0);
        pageResult.setMessage("成功");
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setData(pageInfo.getList());
        log.info("获取课程操作日志成功");
        return pageResult;
    }
    /**
     * 删除日志
     * @ruturn 根据id删除对应的日志数据
     */
    @RequestMapping("/advanced/courseoperatedel")
    public Map<String,String> del(int id){
        log.info("根据id删除对应的日志数据");
        courseOperateLogService.delCourseOperateLog(id,EnumPojo.VALUE_DEL_1);
        Map<String,String> map=new HashMap<>();
        map.put("state","200");
        log.info("根据id删除对应的日志数据成功");
        return map;
    }
    /**
     * 添加课程的日志数据
     * @param courseoperatelog 要添加的日志数据
     */
    @RequestMapping("/advanced/addoperatelog")
    public PageResult addOperateLog(CourseOperateLog courseoperatelog){
        log.info("要添加课程的日志数据");
        Timestamp time = new Timestamp(new Date().getTime());
        courseoperatelog.setOperatetime(time);
        String name = (String) WebUtils.getSession().getAttribute("usernamename");
        courseoperatelog.setName(name);
        courseOperateLogService.addCourseOperateLog(courseoperatelog);
        PageResult pageResult=new PageResult();
        pageResult.setState(200);
        log.info("要添加课程的日志数据成功");
        return pageResult;
    }
}
